package com.jumi.microservice.common.utils;

import com.jumi.microservice.common.base.dto.BasePageResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.ResultEnum;

import java.util.Collection;

/**
 * <p>
 * 响应结果生成工具
 * </p>
 *
 * @author liuz
 * @since 2020-07-18
 */
public class ResultGenerator {
    private static final String DEFAULT_SUCCESS_MESSAGE = "操作成功!";

    public static ResponseResult genSuccessResult() {
        return new ResponseResult()
                .setCode(ResultEnum.SUCCESS.getCode())
                .setMessage(DEFAULT_SUCCESS_MESSAGE);
    }

    public static <T> ResponseResult<T> genSuccessResult(T data) {
        return new ResponseResult()
                .setCode((ResultEnum.SUCCESS.getCode()))
                .setMessage(DEFAULT_SUCCESS_MESSAGE)
                .setData(data);
    }

    public static <T> BasePageResponse<T> genSuccessResultPage(T data, Long total) {
        return new BasePageResponse()
                .setCode((ResultEnum.SUCCESS.getCode()))
                .setMessage(DEFAULT_SUCCESS_MESSAGE)
                .setRows(data)
                .setTotal(total);
    }

    public static <T> BasePageResponse<T> genSuccessResultPage(T data, Long total, Long pageCount) {
        return new BasePageResponse()
                .setCode((ResultEnum.SUCCESS.getCode()))
                .setMessage(DEFAULT_SUCCESS_MESSAGE)
                .setRows(data)
                .setTotal(total)
                .setPageCount(pageCount);
    }

    public static ResponseResult genFailResult(String message) {
        return new ResponseResult()
                .setCode(ResultEnum.FAIL.getCode())
                .setMessage(message);
    }
}
